﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Web.Security;

namespace BeniBastanYaratProject
{
    public partial class KategoriDetay : System.Web.UI.Page
    {
        DBBaglantiDataContext db = new DBBaglantiDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack) return;
            if (Request.QueryString["kid"] != null)
            {
                try
                {
                    if (Request.QueryString["Page"] != null)
                    {
                        ViewState["PageID"] = Request.QueryString["Page"];
                    }
                    else
                    {
                        ViewState["PageID"] = 0;
                    }
                    int KatID = int.Parse(Request.QueryString["kid"]);

                    ViewState["KatID"] = KatID;
                    KategoriUrunleriDoldur(KatID);
                }
                catch (Exception)
                {

                }
            }
            else if (Request.QueryString["mid"] != null)
            {
                try
                {
                    if (Request.QueryString["Page"] != null)
                    {
                        ViewState["PageID"] = Request.QueryString["Page"];
                    }
                    else
                    {
                        ViewState["PageID"] = 0;
                    }
                    int MarkaID = int.Parse(Request.QueryString["mid"]);

                    ViewState["MarkaID"] = MarkaID;
                    MarkayaGoreDoldur(MarkaID);
                }
                catch (Exception)
                {

                }

            }
        }

        private void KategoriUrunleriDoldur(int KatID)
        {
            int StartRow = ViewState["PageID"].ToString() == "0" ? 0 : ((int.Parse(ViewState["PageID"].ToString()) * 12) + 1);
            var sorgu = (from x in db.Urunlers where x.KategoriID == KatID select new { x.UrunID, x.UrunAdi, x.UrunResmiKucuk, UrunSatisFiyati = x.UrunSatisFiyati.ToString() + " TL", x.Kategoriler.KategoriAdi }).Skip(StartRow).Take(12);
            DataList1.DataSource = sorgu;
            DataList1.DataBind();

            CreatePagerButtons();



        }

        private void MarkayaGoreDoldur(int MarkaID)
        {
            int StartRow = ViewState["PageID"].ToString() == "0" ? 0 : ((int.Parse(ViewState["PageID"].ToString()) * 12) + 1);
            var sorgu = (from x in db.Urunlers where x.MarkaID == MarkaID select new { x.UrunID, x.UrunAdi, x.UrunResmiKucuk, UrunSatisFiyati = x.UrunSatisFiyati.ToString() + " TL", x.Kategoriler.KategoriAdi }).Skip(StartRow).Take(12);
            DataList1.DataSource = sorgu;
            DataList1.DataBind();

            MarkaCreatePagerButtons();



        }

        private void MarkaCreatePagerButtons()
        {
            int TotalItemCount = (from x in db.Urunlers where x.MarkaID == int.Parse(Request.QueryString["mid"]) select x).Count();
            int PageCount = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(TotalItemCount) / 12));
            for (int i = 0; i < PageCount; i++)
            {
                PagerDiv.InnerHtml += "<a  class=\"PageCount\" href='" + Request.Url.AbsolutePath + "?mid=" + ViewState["MarkaID"] + "&Page=" + (i) + "'>" + (i + 1) + "</a>&nbsp;&nbsp;&nbsp;&nbsp;";
            }
        }


        private void CreatePagerButtons()
        {
            int TotalItemCount = (from x in db.Urunlers where x.KategoriID == int.Parse(Request.QueryString["kid"]) select x).Count();
            int PageCount = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(TotalItemCount) / 12));
            for (int i = 0; i < PageCount; i++)
            {
                PagerDiv.InnerHtml += "<a  class=\"PageCount\" href='" + Request.Url.AbsolutePath + "?kid=" + ViewState["KatID"] + "&Page=" + (i) + "'>" + (i + 1) + "</a>&nbsp;&nbsp;&nbsp;&nbsp;";
            }
        }

        protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "SepeteEkle")
            {

                if (Membership.GetUser() != null)
                {
                    bool TransactionIsSuccessful = SepetIslemleri.SepeteEkle(Convert.ToInt32(e.CommandArgument), int.Parse("1"), new Guid(Membership.GetUser().ProviderUserKey.ToString()));
                    if (TransactionIsSuccessful)
                    {
                        ClientScript.RegisterStartupScript(typeof(Page), "Information", "alert(\"Urun Sepete Eklendi\");", true);
                        Master.SepetHesapla();

                    }
                    else
                    {
                        ClientScript.RegisterStartupScript(typeof(Page), "Error", "alert(\"Urun Sepete Ekleme Başarısız..\");", true);
                    }
                }
                else
                {
                    Response.Redirect("~/Login.aspx");
                }
            }
        }


    }
}